package com.ruoyi.project.modules.businessSystem.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.modules.businessSystem.domain.BusinessSystem;
import com.ruoyi.project.modules.businessSystem.service.IBusinessSystemService;
import com.ruoyi.project.modules.table.domain.TableInfo;
import com.ruoyi.project.modules.table.service.ITableInfoService;
import com.ruoyi.project.modules.tableObject.domain.TableObject;
import com.ruoyi.project.modules.tableObject.service.ITableObjectService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 业务系统Controller
 *
 * @author smallrain
 * @date 2021-11-06
 */
@Controller
@RequestMapping("/businessSystem/businessSystem/new")
public class BusinessSystemNewController extends BaseController {

    private String prefix = "businessSystem/businessSystem/new";

    @Autowired
    private IBusinessSystemService businessSystemService;

    @Autowired
    private ITableObjectService tableObjectService;

    @Autowired
    private ITableInfoService tableInfoService;

    @RequiresPermissions("businessSystem:businessSystem:view")
    @GetMapping()
    public String businessSystem() {
        return prefix + "/businessSystem";
    }

    @PostMapping("/tableList")
    @ResponseBody()
    public AjaxResult tableList(@RequestParam String categoryCode, @RequestParam Long businessId) {
        List<TableObject> tableObjects;
        if (null != businessId) {
            TableObject tableObject = new TableObject();
            tableObject.setObjectId(businessId);
            tableObject.setObjectType("1");
            tableObjects = this.tableObjectService.selectTableObjectList(tableObject);
            if (CollectionUtil.isEmpty(tableObjects)) {
                return AjaxResult.success();
            }
        } else {
            BusinessSystem bs = new BusinessSystem();
            bs.setCategoryCode(categoryCode);
            List<BusinessSystem> bsList = businessSystemService.selectBusinessSystemList(bs);
            if (CollectionUtil.isEmpty(bsList)) {
                return AjaxResult.success();
            }
            TableObject tableObject = new TableObject();
            tableObject.setObjectIds(bsList.stream()
                    .map(BusinessSystem::getId)
                    .collect(Collectors.toSet()));
            tableObject.setObjectType("1");
            tableObjects = this.tableObjectService.selectTableObjectList(tableObject);
            if (CollectionUtil.isEmpty(tableObjects)) {
                return AjaxResult.success();
            }
        }
        List<TableInfo> result = tableInfoService.selectTableInfoByTableIds(tableObjects.stream()
                .map(TableObject::getTableId)
                .collect(Collectors.toList()));
        return AjaxResult.success(result);
    }
}
